/*
 * @Author: wangjh wangjiahui0710@163.com
 * @Date: 2024-05-20 16:40:44
 * @LastEditors: wangjh wangjiahui0710@163.com
 * @LastEditTime: 2024-06-12 18:58:07
 * @FilePath: \hn-fire-product-mobile\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import type { App } from 'vue';
import { createRouter, createWebHashHistory } from 'vue-router/auto';

import { useUserStore } from '@/stores/user';

/**
 *
 * 根据路径自动生成定制化路由
 * unplugin-vue-router
 * https://github.com/posva/unplugin-vue-router
 */

export const router = createRouter({
	history: createWebHashHistory(),
	//重写自动生成的路由
	extendRoutes: (routes) => {
		routes.push({
			path: '/',
			redirect: {
				path: '/home',
			},
		});
		routes.push({
			path: '/danger',
			component: () => import('../pages/home/danger.vue'),
		});
		return routes;
	},
});

router.beforeEach((to, from, next) => {
	const userStore = useUserStore();
	if (to.path === '/login/register') {
		return next();
	} else if (to.path === '/login/createUnit') {
		return next();
	} else if (to.path === '/login/registerTable') {
		return next();
	} else if (to.path !== '/login' && !userStore.isLogined) {
		return next({ path: '/login' });
	}
	return next();
});

export function setupRouter(app: App) {
	return app.use(router);
}
